Certified Java Programmer Mock Exam


Question 1


abstract class A {             // 1
  abstract static void m1();   // 2
  abstract static class B {}   // 3
  static class C extends B {}  // 4
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 2


abstract class A {
  private int x = 4;
  private int y = 2;
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
  public abstract int math();
}
class B {
  static A a1 = new A() {public int math() {return x()+y();}}
  static A a2 = new A() {public int math() {return x()-y();}}
  static A a3 = new A() {public int math() {return x()*y();}}
  static A a4 = new A() {public int math() {return x()/y();}}
  public static void main(String[] args) {
    System.out.print("" + a1.math() +
                          a2.math() +
                          a3.math() +
                          a4.math());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 18
b. Prints: 6282
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 3


class Z {
  void m1() {
    abstract class A {}      // 1
    final class B {}         // 2
    private class C {}       // 3
    protected class D {}     // 4
    public class E {}        // 5
    static class F {}        // 6
    synchronized class G {}  // 7
    transient class H {}     // 8
    volatile class I {}      // 9
    strictfp class J {}      // 10
  }
}

Which of the follow class declarations results in a compiler error?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. 10
k. None of the Above

Question 4


abstract class A {
  private int x = 4;
  private int y = 2;
  public A(int i1) {x=i1;}
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
  public void incY() {y++;}
  public abstract int math();
}
class B {
  static A a1 = new A(2) {
    {incY();}
    public int math() {return x()+y();}
  };
  public static void main(String[] args) {
    System.out.print(a1.math());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 5
b. Prints: 6
c. Prints: 7
d. Compile-time error.
e. Run-time error.
f. None of the Above.

Question 5


abstract class A {
  private int x = 4;
  private int y = 2;
  public A(int i1, int i2) {x=i1;y=i2;}
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
  public abstract int math();
}
class B {
  static A a1 = new A(2,1) {public int math() {return x()+y();}};
  static A a2 = new A(2,1) {public int math() {return x()-y();}};
  static A a3 = new A(2,1) {public int math() {return x()*y();}};
  static A a4 = new A(2,1) {public int math() {return x()/y();}};
  public static void main(String[] args) {
    System.out.print("" + a1.math() +
                          a2.math() +
                          a3.math() +
                          a4.math());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 8
b. Prints: 3122
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 6


abstract class A {                   // 1
  abstract synchronized void m1();   // 2
  abstract synchronized class B {}   // 3
  synchronized class C extends B {}  // 4
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 7



Which of the follow are true statements.

a. A local class is declared within a block.
b. An anonymous class is always a local class.
c. A local class is a nested class.
d. A local class is a member class.
e. A local class is always a named class.
f. None of the above.

Question 8


class A {
  int B;        // 1
  void B() {}   // 2
  class B {}    // 3
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 9


abstract class A {
  private int x = 4;
  private int y = 2;
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
}
interface B {int math();}
class C {
  static class D extends A implements B {
    public int math() {return x()+y();}
  }
  static A a1 = new A() implements B {
    public int math() {return x()+y();}
  };
  public static void main(String[] args) {
    System.out.print(new C.D().math());
    System.out.print(a1.math());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 12
b. Prints: 66
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 10


class A {
  private static int f1 = 1;
  private int f2 = 2;
  void m1(int p1, final int p2) {
    int l1 = 5;
    final int l2 = 6;
    Object x = new Object() {
      int a = f1;  // 1
      int b = f2;  // 2
      int c = p1;  // 3
      int d = p2;  // 4
      int e = l1;  // 5
      int f = l2;  // 6
    };

  }
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. None of the Above.

Question 11



Which of the following modifiers can be applied to a local class?

a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 12


class D {
  D() {System.out.print("D");}
  class Z {Z(){System.out.print("Z");}}
  public static void main(String args[]) {
    new D.Z();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: D
b. Prints: Z
c. Prints: DZ
d. Prints: ZD
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 13


class E {
  E() {System.out.print("E");}
  static class Z {Z(){System.out.print("Z");}}
  public static void main(String args[]) {
   new E.Z();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: E
b. Prints: Z
c. Prints: EZ
d. Prints: ZE
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 14



Which of the follow are true statements.

a. An anonymous class can extend only the Object class.
b. An anonymous class can not implement an interface.
c. An anonymous class can be abstract.
d. An anonymous class is implicitly final.
e. An anonymous class can be static.
f. The class instance creation expression for an anonymous class must never include parameters.
g. An anonymous class must declare at least one constructor.
h. None of the above.

Question 15


1.  class F {
2.    public void m1() {Z.m1();}
3.    private static class Y {
4.      private static void m1() {
5.        System.out.print("Y.m1 ");
6.      }
7.    }
8.    private static class Z {
9.      private static void m1(){
10.       System.out.print("Z.m1 ");
11.       Y.m1();
12.     }
13.   }
14.   public static void main(String[] args) {
15.     new F().m1();
16.   }
17. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 2
b. Compiler error at line 11
c. Runtime error at line 2
d. Runtime error at line 11
e. Prints: Z.m1 Y.m1
f. None of the Above

Question 16


class C {
  private static String s1 = "s1";
  String s2 = "s2";
  C() {m1("s5","s6");}
  void m1(final String s5, String s6) {
    final String s3 = "s3"; String s4 = "s4";
    class Z {
      Z() {
        System.out.print(???);
      }
    }
    new Z();
  }
  public static void main(String args[]) {new C();}
}

Which variables can be substituted for ??? without causing a compiler error?

a. s1 
b. s2 
c. s3 
d. s4 
e. s5 
f. s6 
g. Runtime error
h. Compiler error
i. None of the above

Question 17


1.  class Red {
2.    private static final int a = 10;
3.    protected static int b = 20;
4.    int c = 30;
5.    static class StaticNested {
6.      int d = a;
7.      int e = b;
8.      int f = c;
9.    }
10.   class NonStaticInner {
11.     int g = a;
12.     int h = b;
13.     int i = c;
14.   }
15. }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. Compiler error at line 8.
h. Compiler error at line 10.
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1